package com.zeldu.exam.controller;

import com.zeldu.common.model.response.CommonCode;
import com.zeldu.common.model.response.QueryResponseResult;
import com.zeldu.common.model.response.QueryResult;
import com.zeldu.exam.pojo.Course;
import com.zeldu.exam.service.CourseService;
import com.zeldu.exam.pojo.*;
import com.zeldu.exam.service.CreatePaperAutoService;
import com.zeldu.exam.service.StudentExamService;
import com.zeldu.exam.service.examService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("exam")
public class examController {
    @Autowired
    private examService examService;
    @Autowired
    private StudentExamService studentExamService;
    @Autowired
    private CreatePaperAutoService createPaperAutoService;
    @Autowired
    private CourseService courseService;
    /*
    * 返回json，老师去找exams
    * */
    @RequestMapping(value = "yourexam/{teacher_id}",method = {RequestMethod.GET})
    @ResponseBody
    public QueryResponseResult<exam> findYouExam(@PathVariable("teacher_id") Long teacher_id){

        List<exam> list = examService.queryByTeacherId(teacher_id);
        QueryResult<exam> queryResult=new QueryResult<>();
        queryResult.setList(list);
        queryResult.setTotal(list.size());
        if(list!=null){
            return  new QueryResponseResult<exam>(CommonCode.SUCCESS,queryResult);
        }else {
            return  new QueryResponseResult<exam>(CommonCode.FAIL,queryResult);
        }
    }

    @RequestMapping(value = "All/{teacher_id}",method = {RequestMethod.GET})
    @ResponseBody
    public List<Course> findAllCourse(@PathVariable("teacher_id") Long teacher_id){

        List<Course> list = courseService.queryTeacherAll(teacher_id);
        System.out.print("************"+list.toString());
        return list;
    }


//    设置考试同时生成试卷
//    通过subject获取到题库中的题型，，生成paperid 将前台信息插入exam并获取exam_id，将题插入中间表生成试卷
    @RequestMapping(value = "insert",method = {RequestMethod.POST})
    @ResponseBody
    public String InsertExam(@RequestBody exam exam){

        exam.toString();
        String subject = exam.getSubject();
        List<questionLibrary> list = createPaperAutoService.findQuestionBySubject(subject);
        System.out.print(list);
        long timeNew =  System.currentTimeMillis();//paper_id
        exam.setPaperid(timeNew);
        Timestamp d = new Timestamp(System.currentTimeMillis());
        exam.setCreatetime(d);
//        -----未测试--------
        exam.setSubject(subject);
//        ----------------
        examService.insert(exam);
        Long exid = exam.getId();

//        System.out.print("@@@@@@@@@@@@@@@"+exid);
//          题库插入无问题
        if (list.size()<20){

            for(int i =0;i<list.size();i++){
                questionLibrary questionLibrary = list.get(i);
                exam_question ex = new exam_question();
                ex.setPaper_id(timeNew);
                ex.setQuestion_id(questionLibrary.getId());
                ex.setExam_id(exid);
                createPaperAutoService.insertPaper(ex);
            }
        }else {
            for (int i = 0;i<20;i++){
                questionLibrary questionLibrary = list.get(i);
                exam_question ex = new exam_question();
                ex.setPaper_id(timeNew);
                ex.setQuestion_id(questionLibrary.getId());
                ex.setExam_id(exid);
                createPaperAutoService.insertPaper(ex);

            }
        }

// -------------------------------------------------------
        //当与course联系上后可以通过couse获取学生名单插入
//--------------------------------------------------------
        System.out.print("@@@@@@@@@@@@@@@"+exam.getCourseid());
        Long id = exam.getCourseid();
        List<Course> courses = courseService.queryStudentsId(id);
        System.out.print("=============="+courses.toString());
        for (Course co : courses){
            StudentExam st = new StudentExam();
            st.setExamId(exid);
            st.setStatus(0);
            st.setScore(0);
            st.setStudentId(co.getStudentId());
            studentExamService.insert(st);
        }

        return "创建成功";
    }

    @RequestMapping(value = "delete/{id}",method = {RequestMethod.GET})
    @ResponseBody
    public int delete(@PathVariable("id") Long id){
        return examService.deleteById(id);
    }

    @RequestMapping(value = "update",method = {RequestMethod.POST})
    @ResponseBody
    public int update(@RequestBody exam exam){
        return examService.updateById(exam);
    }

    @RequestMapping(value = "findById/{id}",method = {RequestMethod.GET})
    @ResponseBody
    public QueryResponseResult<exam> findById(@PathVariable("id") Long id){
        exam ex = examService.queryById(id);
        List<exam> list = new ArrayList<exam>();
        list.add(ex);
        QueryResult<exam> queryResult=new QueryResult<>();
        queryResult.setList(list);
        queryResult.setTotal(list.size());
        return  new QueryResponseResult<exam>(CommonCode.SUCCESS,queryResult);
    }


//    学生的exam页面展示
    @RequestMapping(value = "studentExam/{id}",method = {RequestMethod.GET})
    @ResponseBody
    public QueryResponseResult<ExamInfoAndStatus> studentExam(@PathVariable("id") Long id){
        List<StudentExam> list = studentExamService.queryBystudentId(id);
        List<exam> list1 = new ArrayList<exam>();
        List<ExamInfoAndStatus> exinfo = new ArrayList<ExamInfoAndStatus>();
        int i = 0;
        for(StudentExam li:list) {
            list1.add(examService.queryById(li.getExamId()));
            ExamInfoAndStatus ex = new ExamInfoAndStatus();
            ex.setCreatetime(list1.get(i).getCreatetime());
            ex.setId(list1.get(i).getId());
            ex.setEndtime(list1.get(i).getEndtime());
            ex.setPapername(list1.get(i).getPapername());
            ex.setStarttime(list1.get(i).getStarttime());
            ex.setSubject(list1.get(i).getSubject());
            ex.setStatus(li.getStatus());
            ex.setScore(li.getScore());
            exinfo.add(ex);
            i++;
        }
        QueryResult<ExamInfoAndStatus> queryResult=new QueryResult<>();
        queryResult.setList(exinfo);
        queryResult.setTotal(exinfo.size());
        return  new QueryResponseResult<ExamInfoAndStatus>(CommonCode.SUCCESS,queryResult);
    }


}
